/*
 * Copyright (c) 2016. 恒昌互联网运营中心.
 */
package com.credithc.eagleeye.plugins.redis;
import com.credithc.eagleeye.agent.factory.EagleEyeAgentFactory;
import com.credithc.eagleeye.constants.RpcTypeEnum;
import com.credithc.eagleeye.thread.EagleEyeThreadContext;
import com.credithc.eagleeye.thread.TraceInfo;
import com.credithc.eagleeye.util.FastJsonConvert;
import com.credithc.eagleeye.vo.RpcTraceInfoVo;
import org.apache.commons.lang3.time.FastDateFormat;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * 描 述: 鹰眼WebService方法<br/>
 * 作 者: zhangjin<br/>
 * 创 建：2016年09月27日<br/>
 * 版 本：v1.0.0<br>
 * <p/>
 * 历 史: (版本) 作者 时间 注释 <br/>
 */
@Deprecated
@Aspect
public class EagleEyeRedisUtilAspect {
    private transient static final Logger logger = LoggerFactory.getLogger(EagleEyeRedisUtilAspect.class);
    private static final FastDateFormat ISO_DATETIME_TIME_ZONE_FORMAT_WITH_MILLIS = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSZZ");

    @Pointcut(value = "execution(* com.credithc.framework.redis.util.RedisClusterUtil.*(..)) && args(key,..)")
    public void pointcut(String key) {
    }
    @Around(value = "pointcut(key)")
    public Object around(ProceedingJoinPoint pjp, String key) throws Throwable {
        TraceInfo traceInfo = EagleEyeThreadContext.getTraceInfo();
        if (traceInfo == null) {
            traceInfo = new TraceInfo();
        } else {
            traceInfo.addSequenceNo();
        }
        long beginTime = System.currentTimeMillis();
        Object resultObject=null;
        RpcTraceInfoVo rpcTraceInfoVo = new RpcTraceInfoVo();
        try {
            rpcTraceInfoVo.setRequestDateTime(ISO_DATETIME_TIME_ZONE_FORMAT_WITH_MILLIS.format(new Date()));
            rpcTraceInfoVo.setTraceId(traceInfo.getTraceId());
            rpcTraceInfoVo.setRpcId(traceInfo.getRpcId());
            rpcTraceInfoVo.setRpcType(RpcTypeEnum.REDIS.getRpcName());
            rpcTraceInfoVo.setServiceCategory("Redis");
            rpcTraceInfoVo.setServiceName(pjp.getTarget().getClass().getSimpleName());
            rpcTraceInfoVo.setMethodName(pjp.getSignature().getName());
            List<Object> argsList = new ArrayList<>();
            for(Object arg : pjp.getArgs()) {
                argsList.add(arg);
            }
            rpcTraceInfoVo.setRequestJson(FastJsonConvert.convertObjectToJSON(argsList));
            rpcTraceInfoVo.setUserInfo(traceInfo.getUserInfo());
            rpcTraceInfoVo.setServerHost("");
            //执行调用
            resultObject = pjp.proceed();
            rpcTraceInfoVo.setRunTime(System.currentTimeMillis()-beginTime);
            rpcTraceInfoVo.setResult("OK");//OK
            rpcTraceInfoVo.setResponseJson(FastJsonConvert.convertObjectToJSON(resultObject));
        }catch (Exception ex){
            rpcTraceInfoVo.setResult("ERROR");//ERROR
            throw ex;
        }finally {
            EagleEyeAgentFactory.getInstance().send2EagleEye(rpcTraceInfoVo);
        }

        return resultObject;
    }
}
